package com.zw.mes.module.cost.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.cost.CosProcess;
import com.zw.mes.entity.cost.CosProcessDetail;
import com.zw.mes.module.cost.model.CosSheet;
import com.zw.mes.module.cost.model.DsnProStruct;
import com.zw.mes.module.cost.model.Project;
import com.zw.mes.module.cost.service.CostService;
import com.zw.mes.module.cost.service.DictCommonService;
//import com.zw.mes.module.cost.service.ProductionSheetService;
import com.zw.mes.module.cost.service.SheetService;
import com.zw.mes.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 机加成本业务
 * <p>
 * 权限：
 * dsn:proTasks:view 任务项编辑查看
 * dsn:proTasks:edit 编辑、保存
 * dsn:proTaskinfor:view 任务项查询
 * @Author: China.wsg
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/craft/cost/machining")
@SuppressWarnings("all")
public class CraCostMachiningController extends BaseController {

    @Autowired
    private CostService costService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
//    @Autowired
//    private ProductionSheetService productionSheetService;

    @GetMapping("/subList")
    public String subPage() {
        return "designtask/list/costSubPage";
    }

    /**
     * 机加录入
     *
     * @param model
     * @return
     */
    @GetMapping("/create")
    @RequiresPermissions({"cost:machining:view"})
    public String createPage(Model model) {
        model.addAttribute("specials",dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("processes", dictCommonService.findProcesses());
        model.addAttribute("equNames", dictCommonService.findEqu());
        return "cost/machining/machiningPage";
    }

    /** myf查询制作件清单列表*/
    @ResponseBody
    @PostMapping("/processList")
    public JsonResult processList(){
        List<CosProcess> cosProcessList = costService.findProcesses();
        return jsonResult(cosProcessList);

    }
    /** 根据工序查询设备*/
    @ResponseBody
    @PostMapping("/selectBuf")
    public JsonResult selectBuf(String name){
        List<CosProcessDetail> cosProcessList = costService.findBufByName(name);
        return jsonResult(cosProcessList);

    }

    /**
     * 机加查询、提交、修改
     *
     * @param model
     * @return
     */
    @GetMapping("/submit")
    @RequiresPermissions({"cost:machiningPlan:view"})
    public String submit(Model model) {

        return "cost/machining/machiningSubmit";
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     *
     * @param param
     * @return
     */
    @PostMapping("/codeExist")
    @ResponseBody
    public String codeExist(String proCode) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", costService.codeExist(proCode));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     *
     * @param param
     * @return
     */
    @GetMapping("/project")
    @ResponseBody
//    @RequiresPermissions({"dsn:proTasks:view"})
    public List<Project> findProject(String proCode) {
        return costService.findProjectByCode(proCode);
    }
    /** myf根据项目编号和专业类型查询部分 */
    @GetMapping("/part/list")
    @ResponseBody
    public List<DsnProStruct> partList(String code, String type) {
        List<DsnProStruct> list = sheetService.findPartByProjectCode(code, type);
        return list;
    }

    /**
     * 保存机加录入
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"cost:machining:edit"})
    public JsonResult saveMake(CosSheet cosSheet, String[] drawNo, String[] drawName, Integer[] drawNum, String[] process, String[] equName, Double[] quantity, String[] remark) {
        cosSheet.setCostType(9);//机加工时计划单
        if (drawNo == null && drawName == null) {
            return JsonResult.success("请最少录入一条数据！", -1);
        }
        int i = costService.saveMachining(cosSheet, drawNo, drawName, drawNum, process, equName, quantity,remark);
        if (-2 == i) {
            return JsonResult.success("当前项目下的专业和部分已经提报过了！请走变更流程！", -2);
        }
        return jsonResult(1);
    }

    /**
     * 保存机加修改
     */
    @ResponseBody
    @PostMapping("/saveEdit")
    @RequiresPermissions({"cost:machiningPlan:edit"})
    public JsonResult saveEdit(CosSheet cosSheet, String[] drawNo, String[] drawName, Integer[] drawNum,  String[] process, String[] equName,Double[] quantity, String[] remark) {
        cosSheet.setCostType(9);//机加工时计划单

        if (drawNo == null && drawName == null) {
            return JsonResult.success("请最少录入一条数据！", -1);
        }
        int i = costService.saveEditMachining(cosSheet, cosSheet, drawNo, drawName, drawNum, process, equName, quantity,remark);
        if (-2 == i) {
            return JsonResult.success("当前项目下的专业和部分已经提报过了！请走变更流程！", -2);
        }
        return jsonResult(1);
    }


    @ResponseBody
    @PostMapping("/saveUpdate")
//    @RequiresPermissions({"cost:machiningPlan:edit"})
    public JsonResult saveUpdate(CosSheet cosSheet, String[] drawNo, String[] drawName, Integer[] drawNum,  String[] process, String[] equName,Double[] quantity, String[] remark) {
        cosSheet.setCostType(9);//机加工时计划单
        cosSheet.setStatus(1);
        cosSheet.setVerifyId(null);
        cosSheet.setVerifyTime(null);
        cosSheet.setVerifyCode(null);
        if (drawNo == null && drawName == null) {
            return JsonResult.success("请最少录入一条数据！", -1);
        }
        int i = costService.saveUpdateMachining(cosSheet, cosSheet, drawNo, drawName, drawNum, process, equName, quantity,remark);
        if (-2 == i) {
            return JsonResult.success("当前项目下的专业和部分已经提报过了！请走变更流程！", -2);
        }
        return jsonResult(1);
    }

    /**
     * 列表查询
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(CosSheet cosSheet) {
        startPage();
        cosSheet.setCreateCode(ShiroUtils.getUser().getAccount());
        cosSheet.setCostType(9);// 机加工时计划单
        List<CosSheet> list = costService.findCosSheetByQuery(cosSheet);
        return jsonResult(list);
    }

    /**
     * 机加修改
     *
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"cost:machiningPlan:edit"})
    public String edit(@PathVariable("id") Integer id, Model model) {
        CosSheet cosSheet = costService.getCosSheetById(id);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("proPart", sheetService.findPartByProjectCode(cosSheet.getProCode(), "外观".equals(cosSheet.getSpecial()) ? null : cosSheet.getSpecial()));
        model.addAttribute("processes", dictCommonService.findProcesses());
        model.addAttribute("equNames", dictCommonService.findEqu());
        model.addAttribute("cosMachiningDetail", costService.findMachiningCosDetailByCosSheetIdAndDataType(id, "'机加工时'"));
        return "cost/machining/machiningEdit";
    }

    /**
     * 机加删除
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"cost:machining:del"})
    public JsonResult del(Integer id, Model model) {

        return jsonResult(costService.delCostById(id, "机加工时计划单"));
    }

    /**
     * 机加提交
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions({"cost:machining:commit"})
    public JsonResult submit(Integer id, Model model) {

        return jsonResult(costService.submitCost(id, "机加工时计划单"));
    }

    /**
     * 机加查询明细页面
     *
     * @param model
     * @return
     */
    @GetMapping("/queryDetail/{id}")
    public String queryDetail(@PathVariable("id") Integer id, Model model) {
        CosSheet cosSheet = costService.getCosSheetById(id);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("cosMachiningDetail", costService.findMachiningCosDetailByCosSheetIdAndDataType(id, "'机加工时'"));
        return "cost/machining/machiningQueryDetail";
    }

    /**
     * 机加工时计划单审核页面
     *
     * @return
     */
    @GetMapping("/examine")
    @RequiresPermissions({"cost:machiningList:view"})
    public String checkPage() {
        return "cost/machining/machiningExamine";
    }

    /**
     * 待审核列表
     */
    @ResponseBody
    @PostMapping("/examine")
    public JsonResult checkList(CosSheet cosSheet) {
        startPage();
        cosSheet.setCostType(9);// 机加工时计划单
        cosSheet.setPageQuery(3);// 审核页面
        cosSheet.setVerifyId(ShiroUtils.getUser().getId());
        List<CosSheet> list = costService.findCosSheetByQuery(cosSheet);
        return jsonResult(list);
    }

    /**
     * 审核
     */
    @ResponseBody
    @PostMapping("/check")
    @RequiresPermissions({"cost:machiningList:success"})
    public JsonResult checkCost(Integer id) {
        return jsonResult(costService.checkCost(id, "机加工时计划单"));
    }

    /**
     * 驳回
     *
     * @param cosSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"cost:machiningList:success"})
    public JsonResult rejectCost(Integer id, String rejectReason) {
        return jsonResult(costService.rejectCost(id, rejectReason, "机加工时计划单"));
    }

//    /**
//     * 撤销
//     */
//    @ResponseBody
//    @PostMapping("/cancel")
//    @RequiresPermissions({"cost:machiningList:success"})
//    public JsonResult cancelCost(Integer id) {
//        return jsonResult(costService.cancelCost(id, "机加工时计划单"));
//    }


}
